package com.zzyk.main.service.impl;

import com.zzyk.main.configuration.exception.DeviceException;
import com.zzyk.main.configuration.exception.UserException;

import com.zzyk.main.mapper.CompanyMapper;
import com.zzyk.main.mapper.DeviceMapper;
import com.zzyk.main.mapper.SysUserDao;
import com.zzyk.main.model.pojo.CommonCount;
import com.zzyk.main.model.pojo.Company;
import com.zzyk.main.model.pojo.Device;
import com.zzyk.main.model.vo.OrganizationInfo;
import com.zzyk.main.model.vo.UserInfo;
import com.zzyk.main.service.CompanyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private SysUserDao sysUserDao;

    @Override
    public int deleteByPrimaryKey(Integer id) throws UserException, DeviceException {
        List<Device> list = deviceMapper.selectAllByEnterpise(id);
        if (list.size() > 0) {
            throw new DeviceException("企业下有设备不能删除，请先删除设备！");
        }
        List<UserInfo> userInfos = sysUserDao.selectUserByEnterpise(id);
        if (userInfos.size() > 0) {
            throw new UserException("企业下有设备不能删除，请先删除设备！");
        }
        return companyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Company record) {
        return companyMapper.insert(record);
    }

    @Override
    public int insertSelective(Company record) {
        return companyMapper.insertSelective(record);
    }

    @Override
    public Company selectByPrimaryKey(Integer id) {
        return companyMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Company record) {
        return companyMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Company record) {
        return companyMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Company> selectEnterpriseByParam(String param, String[] areas) {
        return companyMapper.selectCompanyByParam(param, areas);
    }

    @Override
    public CommonCount getTotal(String[] areaArr) {
        return companyMapper.getTotal(areaArr);
    }

    @Override
    public List<OrganizationInfo> getEnterpriseByIds(String ids) {
        return companyMapper.getByIds(ids);
    }

    /*@Override
    public void updateEnterprise(Company enterprise) throws IOException {
        if(StringUtils.isNotEmpty(enterprise.getLogoFIle().getOriginalFilename())){
            String logoName = fileService.fileUpload(enterprise.getLogoFIle(), logoPath);
            enterprise.setLogo("logo"+logoName);
        }
        if(StringUtils.isNotEmpty(enterprise.getPhotoFile().getOriginalFilename())){
            String photoName = fileService.fileUpload(enterprise.getPhotoFile(), photoPath);
            enterprise.setPhoto("photo"+photoName);
        }
        enterprise.setUpdate_time(DateUtil.date());
        updateByPrimaryKeySelective(enterprise);
    }*/

    /*@Override
    public void saveEnterprise(Company enterprise) throws IOException {
        String logoName = fileService.fileUpload(enterprise.getLogoFIle(), logoPath);
        String photoName = fileService.fileUpload(enterprise.getPhotoFile(), photoPath);
        enterprise.setType(0);
        enterprise.setLogo("logo"+logoName);
        enterprise.setPhoto("photo"+photoName);
        enterprise.setUpdate_time(DateUtil.date());
        companyMapper.insertSelective(enterprise);
        Company c =new Company();
        c.setArea(enterprise.getId()+"");
        c.setId(enterprise.getId());
        companyMapper.updateByPrimaryKeySelective(c);

    }*/
}

